module.exports = (sequelize, DataTypes) => {
  const Activity = sequelize.define('Activity', {
    activity_id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    anchor_id: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    title: {
      type: DataTypes.STRING(100),
      allowNull: false
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    cover_image: {
      type: DataTypes.STRING(255),
      allowNull: true
    },
    start_time: {
      type: DataTypes.DATE,
      allowNull: false
    },
    end_time: {
      type: DataTypes.DATE,
      allowNull: false
    },
    location: {
      type: DataTypes.STRING(100),
      allowNull: false,
      defaultValue: '线上'
    },
    status: {
      type: DataTypes.ENUM('pending', 'published', 'ongoing', 'completed', 'cancelled'),
      defaultValue: 'pending'
    },
    max_participants: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    current_participants: {
      type: DataTypes.INTEGER,
      defaultValue: 0
    },
    tags: {
      type: DataTypes.STRING(255),
      allowNull: true
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false
    }
  }, {
    tableName: 'v_activity',
    timestamps: false,
    underscored: true
  });

  Activity.associate = function(models) {
    Activity.belongsTo(models.Anchor, {
      foreignKey: 'anchor_id',
      as: 'anchor'
    });
    Activity.hasMany(models.Registration, {
      foreignKey: 'activity_id',
      as: 'registrations'
    });
  };

  return Activity;
};
